home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / BM132A.LZH / bm_132a / dview_io.s < prev    next >
Encoding:
Text File  |  1995-11-27  |  11.0 KB  |  555 lines

  1. *-------------------------------------------------------*
  2. * Doom WAD file Parser                    *
  3. *-------------------------------------------------------*
  4. * Written by Dave Murphy in October 1995        *
  5. *-------------------------------------------------------*
  6.  
  7.     include        include\wad_tab.s
  8.  
  9. Angle_Scalar    =    11930465    (65536/360)*65536
  10. Last_Entry        macro
  11.     dc.l    -1
  12.     endm
  13.  
  14. Dir_Entry        macro
  15.     dc.l        \1,\2
  16.     endm
  17.  
  18. Load_Entry        macro    type
  19.     Lea        \1_Array,a6
  20.     bsr        Load_Construct
  21.     bsr        \1_Setup
  22.     endm
  23.  
  24. *-----------------------------------------------------------*
  25. OpenWad:
  26.  
  27.     Cconws    #Message
  28.  
  29.     Fopen    #WadName,#0
  30.     bpl.s    open_ok
  31.     moveq    #1,d0
  32.     bra    Error
  33.  
  34. open_ok:
  35.     move.w    d0,handle
  36.     Fread    handle,#12,#Signature
  37.  
  38.     lea    IWAD_file,a0
  39.     cmp.l    #'IWAD',Signature
  40.     beq.s    read_file
  41.  
  42.     lea    PWAD_file,a0
  43.     cmp.l    #'PWAD',Signature
  44.     beq.s    read_file
  45.  
  46.     moveq    #3,d0            ; WAD file error
  47.     bra    Error
  48.  
  49. read_file:
  50.     Cconws    a0
  51.  
  52.     Cconws    #Dir_info1
  53.     move.l    Directories,d0
  54.     Fixlong    d0
  55.     move.l    d0,dircount
  56.     bsr    Write_decimal
  57.     Cconws    #Dir_info2
  58.     move.l    Direct_Offset,d0
  59.     Fixlong    d0
  60.     Fseek    d0,handle,#0
  61.     lea    Dir_Header,a6
  62.  
  63.     moveq    #0,d7
  64.     
  65. find_level_loop:
  66.     Fread    handle,#Dir_Len,a6
  67.     bsr    Search_Entry
  68.     tst.w    d7
  69.     bne.s    .found
  70.     subq.l    #1,dircount
  71.     bne.s    find_level_loop
  72.     moveq    #4,d0            ; WAD file error
  73.     bra    Error
  74. .found:
  75.  
  76.  
  77.     moveq    #0,d7
  78. read_dir_loop:
  79.     Fread    handle,#Dir_Len,a6
  80.     bsr    Scan_Entry
  81.     cmp.w    #8,d7
  82.     blt.s    read_dir_loop
  83.  
  84. *-------------------------------------------------------*
  85. * Read in constructs found in WAD file            *
  86. *-------------------------------------------------------*
  87.     Load_Entry    Thing
  88.     Load_Entry    Line    
  89.     Load_Entry    Side
  90.     Load_Entry    Vertex
  91.     Load_Entry    Seg
  92.     Load_Entry    SSector
  93.     Load_Entry    Node
  94.     Load_Entry    Sector
  95.     Fclose        handle
  96.  
  97. *-------------------------------------------------------*
  98. * Calculate size of all arrays allocated        *
  99. *-------------------------------------------------------*
  100.     lea    Thing_Array,a0
  101.     moveq    #0,d0
  102.     moveq    #8-1,d7
  103. calc_sizes:
  104.     add.l    Size(a0),d0
  105.     lea    Construct_len(a0),a0
  106.     dbf    d7,calc_sizes
  107.  
  108.     lsr.l    #8,d0        
  109.     lsr.l    #2,d0            ; round down to nearest 1k
  110.  
  111.     bsr    Write_decimal        ; output result
  112.     Cconws    #Allocated
  113.  
  114.     Crawcin
  115.  
  116.     rts
  117. *-------------------------------------------------------*
  118. * Reserve memory, load a constuct             *
  119. *-------------------------------------------------------*
  120.         rsreset
  121. Array        rs.l    1
  122. Offset        rs.l    1
  123. Size        rs.l    1
  124. Number        rs.w    1
  125. Construct_len    rs.w    1
  126.  
  127. Load_Construct:
  128.     move.l    Size(a6),d0
  129.     bsr    Malloc
  130.     move.l    d0,Array(a6)
  131.     Fseek    Offset(a6),handle,#0
  132.     Fread    handle,Size(a6),Array(a6)
  133.     rts
  134.  
  135. Malloc:    move.l    d0,-(sp)
  136.     move.w    #72,-(sp)
  137.     trap    #1
  138.     addq.l    #6,sp
  139.     tst.l    d0
  140.     beq.s    Memory_Error
  141.     rts
  142.  
  143. Memory_Error:
  144.     moveq    #2,d0
  145.     bra    Error
  146.  
  147. *-------------------------------------------------------*
  148. * Scans Directory name and executes appropriate code    *
  149. *-------------------------------------------------------*
  150. * A6 - pointer to header                *
  151. *-------------------------------------------------------*
  152.  
  153. Search_Entry:
  154.     lea    WadLevel,a0
  155.     cmp.l    #-1,(a0)
  156.     beq.s    .got
  157.     lea    Dir_Name(a6),a2
  158. .next:    move.b    (a0)+,d0
  159.     beq.s    .got
  160.     fixcase    d0
  161.     move.b    (a2)+,d1
  162.     fixcase    d1
  163.     cmp.b    d1,d0
  164.     beq.s    .next
  165.     moveq    #0,d7
  166.     rts
  167. .got:    moveq    #1,d7
  168.     rts
  169.  
  170. Scan_Entry:
  171.     lea    Dir_Table,a0
  172.     move.l    Dir_Offset(a6),d5
  173.     Fixlong    d5
  174.     move.l    Dir_Size(a6),d6
  175.     Fixlong    d6
  176. Scan_loop:
  177.     move.l    (a0)+,a1
  178.     cmp.l    #-1,a1
  179.     beq.s    not_found
  180.     lea    Dir_Name(a6),a2
  181.     move.l    (a0)+,a3
  182. Try_name:
  183.     move.b    (a3)+,d0
  184.     beq.s    got_entry
  185.     cmp.b    (a2)+,d0
  186.     beq.s    Try_name
  187.     bra.s    Scan_loop
  188.  
  189. got_entry:
  190.     addq.w    #1,d7
  191.     jmp    (a1)
  192.  
  193. not_found:
  194.     Cconws    #Skipping
  195.     lea    Dir_Name(a6),a0
  196.     Cconws    a0
  197.     Cconws    #CRLF
  198.     rts
  199.  
  200. *-------------------------------------------------------*
  201. * Outputs D0.l as unsigned decimal in ASCII        *
  202. *-------------------------------------------------------*
  203. Write_decimal:
  204.     lea    Ascii_Output,a0
  205. .convert_to_ascii:
  206.     moveq    #0,d1
  207.     divu.l    #10,d1:d0
  208.     add.b    #'0',d1
  209.     move.b    d1,-(a0)
  210.     tst.l    d0
  211.     bne.s    .convert_to_ascii
  212.  
  213.     Cconws    a0
  214.     rts
  215.  
  216. *-------------------------------------------------------*
  217. * Routines to initialise directory constructs        *
  218. *-------------------------------------------------------*
  219. Init_lines:
  220.     lea    Line_Array,a5
  221.     moveq    #Line_len,d0
  222.     bra.s    Init_Entry
  223. Init_sidedefs:
  224.     lea    Side_Array,a5
  225.     moveq    #Side_len,d0
  226.     bra.s    Init_Entry
  227. Init_vertexes:
  228.     lea    Vertex_Array,a5
  229.     moveq    #Vertex_len,d0
  230.     bra.s    Init_Entry
  231. Init_segs:
  232.     lea    Seg_Array,a5
  233.     moveq    #Seg_len,d0
  234.     bra.s    Init_Entry
  235. Init_ssectors:
  236.     lea    SSector_Array,a5
  237.     moveq    #Ssect_len,d0
  238.     bra.s    Init_Entry
  239. Init_nodes:
  240.     lea    Node_Array,a5
  241.     moveq    #Node_len,d0
  242.     bra.s    Init_Entry
  243. Init_sectors:
  244.     lea    Sector_Array,a5
  245.     moveq    #Sector_len,d0
  246.     bra.s    Init_Entry
  247. Init_things:
  248.     lea    Thing_Array,a5
  249.     moveq    #Thing_len,d0
  250. Init_Entry
  251.     move.l    d6,Size(a5)    
  252.     move.l    d5,Offset(a5)
  253.     divu.l    d0,d6
  254.     move.w    d6,Number(a5)
  255.  
  256.     lea    Dir_Name(a6),a0
  257.     Cconws    a0
  258.     Cconws    #divider
  259.     moveq    #0,d0
  260.     move.w    Number(a5),d0
  261.     bsr    Write_decimal
  262.     Cconws    #CRLF
  263.     rts
  264.  
  265. *-------------------------------------------------------*
  266. * Setup Player start position and angle            *
  267. *-------------------------------------------------------*
  268. Thing_Setup:
  269.     move.l    Array(a6),a0
  270.  
  271. Seek_Player:
  272.     move.w    Thing_type(a0),d0
  273.     Fixword    d0
  274.     cmp.w    #1,d0        ; Player 1 start
  275.     beq.s    Got_Player
  276.     lea    Thing_len(a0),a0
  277.     bra.s    Seek_Player
  278.  
  279. Got_Player:
  280.     move.w    Thing_x(a0),d0
  281.     Fixword    d0
  282.     move.w    d0,Px
  283.  
  284.     move.w    Thing_y(a0),d0
  285.     Fixword    d0
  286.     move.w    d0,Py
  287.  
  288.     moveq    #0,d0
  289.     move.w    Thing_angle(a0),d0
  290.     Fixword    d0
  291.     mulu.l    #Angle_Scalar,d0    
  292.     swap    d0
  293.     move.w    d0,Pangle
  294.     move.w    #40,Ph
  295.  
  296.     rts
  297.  
  298. *-------------------------------------------------------*
  299. * Reformat Line Array to Big Endian            *
  300. *-------------------------------------------------------*
  301. Line_Setup:
  302.     move.l    Array(a6),a0
  303.     move.w    NumLines,d7
  304.     bra.s    Fix_Lines
  305. Fix_Line_Loop:
  306.     moveq    #Line_len/2-1,d6
  307. .Fix_one:
  308.     move.w    (a0),d0
  309.     Fixword    d0
  310.     move.w    d0,(a0)+
  311.     dbf    d6,.Fix_one
  312. Fix_Lines:
  313.     dbf    d7,Fix_Line_Loop
  314.     rts
  315.  
  316. *-------------------------------------------------------*
  317. * Reformat Node array to Big Endian & setup Pnode array    *
  318. *-------------------------------------------------------*
  319. Node_Setup:
  320.     moveq    #0,d0
  321.     move.w    NumNodes,d0
  322.     move.w    d0,d7
  323.     subq.w    #1,d7
  324.     move.w    d7,maxnode
  325.     lsl.l    #2,d0
  326.     bsr    Malloc
  327.     move.l    d0,PNode_Array
  328.     move.l    d0,a1
  329.     move.l    Array(a6),a0
  330. Fix_Nodes:
  331.     move.l    a0,(a1)+        ; insert node in Pnode array
  332.     moveq    #Node_len/2-1,d6
  333. .fix_one:
  334.     move.w    (a0),d0
  335.     Fixword    d0
  336.     move.w    d0,(a0)+
  337.     dbf    d6,.fix_one
  338.     dbf    d7,Fix_Nodes
  339.     rts
  340.  
  341. *-------------------------------------------------------*
  342. * Reformat Side array to Big Endian             *
  343. *-------------------------------------------------------*
  344. Side_Setup:
  345.     move.l    Array(a6),a0
  346.     move.w    NumSides,d7
  347.     bra.s    Fix_Sides
  348. fix_side_loop:
  349.     move.w    Side_tm_xoff(a0),d0
  350.     Fixword    d0
  351.     move.w    d0,Side_tm_xoff(a0)
  352.  
  353.     move.w    Side_tm_yoff(a0),d0
  354.     Fixword    d0
  355.     move.w    d0,Side_tm_yoff(a0)
  356.  
  357.     move.w    Side_sector(a0),d0
  358.     Fixword    d0
  359.     move.w    d0,Side_sector(a0)
  360.  
  361.     lea    Side_len(a0),a0
  362. Fix_Sides:
  363.     dbf    d7,fix_side_loop
  364.     rts
  365.  
  366. *-------------------------------------------------------*
  367. * Reformat Vertex array to Big Endian             *
  368. *-------------------------------------------------------*
  369. Vertex_Setup:
  370.     move.l    Array(a6),a0
  371.     move.w    NumVertexs,d7
  372.     bra.s    Fix_Vertexes
  373. fix_vertex_loop:
  374.     move.w    (a0),d0
  375.     Fixword    d0
  376.     move.w    d0,(a0)+
  377.  
  378.     move.w    (a0),d0
  379.     Fixword    d0
  380.     move.w    d0,(a0)+
  381. Fix_Vertexes:
  382.     dbf    d7,fix_vertex_loop
  383.     rts
  384.  
  385. *-------------------------------------------------------*
  386. * Reformat Seg array to Big Endian             *
  387. *-------------------------------------------------------*
  388. Seg_Setup:
  389.     move.l    Array(a6),a0
  390.     move.w    NumSegs,d7
  391.     bra.s    Fix_Segs
  392. Fix_Seg_Loop:
  393.     moveq    #Seg_len/2-1,d6
  394. .Fix_one:
  395.     move.w    (a0),d0
  396.     Fixword    d0
  397.     move.w    d0,(a0)+
  398.     dbf    d6,.Fix_one
  399. Fix_Segs:
  400.     dbf    d7,Fix_Seg_Loop
  401.     rts
  402.  
  403. *-------------------------------------------------------*
  404. * Reformat Ssector array to Big Endian             *
  405. *-------------------------------------------------------*
  406. SSector_Setup:
  407.     move.l    Array(a6),a0
  408.     move.w    NumSSectors,d7
  409.     bra.s    Fix_Ssectors
  410. fix_ssector_loop:
  411.     move.w    (a0),d0
  412.     Fixword    d0
  413.     move.w    d0,(a0)+
  414.  
  415.     move.w    (a0),d0
  416.     Fixword    d0
  417.     move.w    d0,(a0)+
  418. Fix_Ssectors:
  419.     dbf    d7,fix_ssector_loop
  420.     rts
  421.  
  422. *-------------------------------------------------------*
  423. * Reformat Sector array to Big Endian             *
  424. *-------------------------------------------------------*
  425. Sector_Setup:
  426.     move.l    Array(a6),a0
  427.     move.w    NumSectors,d7
  428.     bra.s    Fix_Sectors
  429. fix_sector_loop:
  430.     move.w    Sector_floorht(a0),d0
  431.     Fixword    d0
  432.     move.w    d0,Sector_floorht(a0)    
  433.  
  434.     move.w    Sector_ceilht(a0),d0
  435.     Fixword    d0
  436.     move.w    d0,Sector_ceilht(a0)    
  437.  
  438.     move.w    Sector_light(a0),d0
  439.     Fixword    d0
  440.     move.w    d0,Sector_light(a0)    
  441.  
  442.     move.w    Sector_type(a0),d0
  443.     Fixword    d0
  444.     move.w    d0,Sector_type(a0)    
  445.  
  446.     move.w    Sector_trigger(a0),d0
  447.     Fixword    d0
  448.     move.w    d0,Sector_trigger(a0)    
  449.  
  450.     lea    Sector_len(a0),a0
  451. Fix_Sectors:
  452.     dbf    d7,fix_sector_loop
  453.     rts
  454.  
  455.  
  456. *-----------------------------------------------------------*
  457.     Section Data
  458. *-----------------------------------------------------------*
  459.  
  460. Message:    dc.b    27,'E',27,'f'
  461.         dc.b    'Falcon Doom - a demo for the Bad Mood project.',13,10
  462.         dc.b    'Rebuilt in 68030 by Doug Little & Dave Murphy.',13,10,13,10,0
  463.  
  464. IWAD_file:    dc.b    'Reading IWAD file.',13,10,0
  465. PWAD_file:    dc.b    'Reading PWAD file.',13,10,0
  466. Dir_info1:    dc.b    'This file contains ',0
  467. Dir_info2:    dc.b    ' Directory Entries.',13,10,0
  468. Skipping:    dc.b    'Skipping: ',0
  469. divider:    dc.b    9,': ',0
  470. Allocated:    dc.b    'Kb Allocated for arrays!',13,10,0
  471. CRLF:        dc.b    13,10,0
  472.  
  473. *-----------------------------------------------------------*
  474.  
  475.     ds.b    40
  476. Ascii_Output:    dc.w    0
  477.  
  478.     even
  479. Dir_Table:
  480.     Dir_Entry    Init_things,Things
  481.     Dir_Entry    Init_lines,Linedefs
  482.     Dir_Entry    Init_sidedefs,Sidedefs
  483.     Dir_Entry    Init_vertexes,Vertexes
  484.     Dir_Entry    Init_segs,Segs
  485.     Dir_Entry    Init_ssectors,Ssectors
  486.     Dir_Entry    Init_nodes,Nodes
  487.     Dir_Entry    Init_sectors,Sectors
  488.     Last_Entry
  489.  
  490. Things:        dc.b    'THINGS',0
  491. Linedefs:    dc.b    'LINEDEFS',0
  492. Sidedefs:    dc.b    'SIDEDEFS',0
  493. Vertexes:    dc.b    'VERTEXES',0
  494. Segs:        dc.b    'SEGS',0
  495. Ssectors:    dc.b    'SSECTORS',0
  496. Nodes:        dc.b    'NODES',0
  497. Sectors:    dc.b    'SECTORS',0
  498.  
  499.     even
  500.  
  501. *-----------------------------------------------------------*
  502. * Directory Entry format
  503. *-----------------------------------------------------------*
  504.     rsreset
  505. Dir_Offset:    rs.l    1
  506. Dir_Size:    rs.l    1
  507. Dir_Name:    rs.l    2
  508. Dir_Len:    rs.l    1
  509.  
  510. Dir_Header:    ds.b    Dir_Len
  511.         dc.b    0
  512.  
  513.     even
  514.  
  515. *-----------------------------------------------------------*
  516.     Section Bss
  517. *-----------------------------------------------------------*
  518.  
  519. dircount:    ds.l    1
  520. Signature:    ds.l    1
  521. Directories:    ds.l    1
  522. Direct_Offset:    ds.l    1
  523.  
  524.  
  525. handle:        ds.w    1
  526.  
  527.  
  528. Structure    macro    name
  529. \1_Array    ds.l    1
  530. \1_Offset    ds.l    1
  531. \1_Size        ds.l    1
  532. Num\1s        ds.w    1
  533.         endm
  534.  
  535. *-----------------------------------------------------------*
  536.     Structure    Thing
  537.     Structure    SSector
  538.     Structure    Sector
  539.     Structure    Side
  540.     Structure    Node
  541.     Structure    Line
  542.     Structure    Seg
  543.     Structure    Vertex
  544.     
  545. PNode_Array    ds.l    1
  546.     
  547. *-----------------------------------------------------------*
  548.     Section Text
  549. *-----------------------------------------------------------*
  550.  
  551.     include    errors.s
  552.  
  553.  
  554.  
  555.